#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
    setupDBus();
    loadHistory();
    demoProperty = "init";
}

MainWindow::~MainWindow()
{

}

void MainWindow::setupUI(){
    this->setWindowTitle("Dbus-Server");
    this->resize(400, 400);

    output = new QTextEdit(this);
    output->setReadOnly(true);
    table = new QTableView(this);
    tableModel = new QStandardItemModel(6, 0, this);
    tableModel->setVerticalHeaderLabels({"姓名","年龄","电话","身份证号","住址","体温"});
    table->setModel(tableModel);
    QVBoxLayout* mainLayout = new QVBoxLayout();
    mainLayout->addWidget(table);
    mainLayout->addWidget(output);

    QWidget* centralWidget = new QWidget(this);
    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);
    output->hide();
}

void MainWindow::setupDBus() {
    // 建立dbus连接
    QDBusConnection connection = QDBusConnection::sessionBus();
    // 注册dbus服务名
    if (!connection.registerService(serviceServerName)) {
        qFatal("Unable to register D-Bus service.");
    }
    // 注册dbus服务路径，导出所有槽函数，信号，属性
    if (!connection.registerObject(serviceServerPath, this, QDBusConnection::ExportAllContents)) {
        qFatal("Unable to register D-Bus object.");
    }
}

void MainWindow::receiveMessage(const QString &message){
    output->append("Received: " + message);
    // 解析json
    QJsonDocument jsonDoc = QJsonDocument::fromJson(message.toUtf8());
    QJsonObject json(jsonDoc.object());
    // 存入.json
    saveHistory(json);
    QString name = json.value("name").toString();
    QString age = json.value("age").toString();
    QString phone = json.value("phone").toString();
    QString cid = json.value("cid").toString();
    QString home = json.value("home").toString();
    QString tem = json.value("tem").toString();
    // 判断体温是否高于37.2
    if(tem > "37.2"){
        output->append("发烧了！不能进门");
    }
    // 把信息加入table
    QList<QStandardItem*> info;
    info.append(new QStandardItem(name));
    info.append(new QStandardItem(age));
    info.append(new QStandardItem(phone));
    info.append(new QStandardItem(cid));
    info.append(new QStandardItem(home));
    info.append(new QStandardItem(tem));
    tableModel->appendColumn(info);
}

void MainWindow::loadHistory(){
    QFile file(fileName);
    if(!file.exists()){
        output->append("还没有历史数据文件history.json");
    }
    if(!file.open(QIODevice::ReadOnly)){
        return;
    }
    // 读取历史数据
    QByteArray data(file.readAll());
    file.close();
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonArray jsonArray = jsonDoc.array();
    for(int i = 0; i < jsonArray.size(); ++i){
        QJsonObject json = jsonArray[i].toObject();
        QString name = json.value("name").toString();
        QString age = json.value("age").toString();
        QString phone = json.value("phone").toString();
        QString cid = json.value("cid").toString();
        QString home = json.value("home").toString();
        QString tem = json.value("tem").toString();
        // 初始化到table
        QList<QStandardItem*> info;
        info.append(new QStandardItem(name));
        info.append(new QStandardItem(age));
        info.append(new QStandardItem(phone));
        info.append(new QStandardItem(cid));
        info.append(new QStandardItem(home));
        info.append(new QStandardItem(tem));
        tableModel->appendColumn(info);
    }
}

void MainWindow::saveHistory(const QJsonObject& json){
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly)){
        return;
    }
    QByteArray data(file.readAll());
    file.close();
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonArray jsonArray = jsonDoc.array();
    jsonArray.append(json);
    // 写回原文件，以缩进格式存放
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
        return;
    }
    QJsonDocument newDoc(jsonArray);
    file.write(newDoc.toJson(QJsonDocument::Indented));
    file.close();
}

QString MainWindow::getProperty(){
    return demoProperty;
}

void MainWindow::setProperty(const QString& newProperty){
    if(demoProperty != newProperty){
        demoProperty = newProperty;
        emit demoPropertyChanged(demoProperty);
    }
}
